package com.bw.skill.mvvm.view;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bw.mvvm_common.mvvm.view.BaseActivity;
import com.bw.skill.BR;
import com.bw.skill.R;
import com.bw.skill.SeckillAction;
import com.bw.skill.TimeServer;
import com.bw.skill.databinding.ActivitySkillBinding;
import com.bw.skill.mvvm.viewmodel.SkillViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/26 0026 下午 3:53
 *
 */
class SkillActivity extends BaseActivity<ActivitySkillBinding, SkillViewModel> {
    boolean serviceFlag = false;
    private TimeServer timeServer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.transparentStatusBar(this);
        BarUtils.setStatusBarLightMode(this, true);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (serviceFlag){
            Intent intent = new Intent(this, TimeServer.class);
            bindService(intent,connection,BIND_AUTO_CREATE);
        }
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_skill;
    }

    @Override
    public int initVerable() {
        return BR.skill;
    }
    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            TimeServer.TimeBind bind = (TimeServer.TimeBind) service;
            timeServer = bind.getTimeService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void skill(SeckillAction action){
        if (action.action.equals("SkillListActivity")){
            serviceFlag = true;
            Intent intent = new Intent(this, TimeServer.class);
            intent.putExtra("time",action.nowTime);
            startService(intent);
            Intent bindIntent = new Intent(this, TimeServer.class);
            bindService(bindIntent,connection,BIND_AUTO_CREATE);
        }else if (action.action.equals("SkillListGetNowTime")){
            long nowTime = timeServer.getTime();
            LogUtils.e("nnnnnnnnnnnnnnnnnnnnnnnnnn"+nowTime);
            vm.requestSkill(nowTime);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }
}
